//
//  XUISplitView.m
//  etionUI
//
//  Created by WangJian on 14-12-16.
//  Copyright (c) 2014年 GuangZhouXuanWu. All rights reserved.
//

#import "XUISplitView.h"

@interface XUISplitViewSwitchParam : NSObject

@property (nonatomic, assign) NSInteger index;
@property (nonatomic, assign) UIView *contentView;
@property (nonatomic, assign) XUISplitViewContentStatus curStatus;
@property (nonatomic, assign) XUISplitViewContentStatus newStatus;

@end

@implementation XUISplitViewSwitchParam


@end

#pragma mark ======================================

@interface XUISplitView () <UIScrollViewDelegate>

@property (nonatomic, strong) NSMutableArray *contentViews;

@end

@implementation XUISplitView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        self.contentViews = [NSMutableArray array];
        
        UISwipeGestureRecognizer *rightSwipeGesture = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(receiveRightSwipeGesture:)] autorelease_mm];
        rightSwipeGesture.direction = UISwipeGestureRecognizerDirectionRight;
        [self addGestureRecognizer:rightSwipeGesture];
        
        UISwipeGestureRecognizer *leftSwipeGesture = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(receiveLeftSwipeGesture:)] autorelease_mm];
        leftSwipeGesture.direction = UISwipeGestureRecognizerDirectionLeft;
        [self addGestureRecognizer:leftSwipeGesture];
        
//        for (int i=0; i<8; i++)
//        {
//            UIView *view = [[UIView alloc] initWithFrame:self.bounds];
//            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
//            label.text = [NSString stringWithFormat:@"%d", i];
//            [view addSubview:label];
//            
//            [self addContentView:view];
//        }
    }
    return self;
}

-(void)dealloc
{
    [_contentViews release_mm];
    superdealloc_mm;
}

#pragma mark - add or del content view

- (UIView *)contentViewAtIndex:(NSInteger)index
{
    return index < self.contentViews.count ? self.contentViews[index] : nil;
}

- (void)addContentView:(UIView *)contentView
{
    [self.contentViews addObject:contentView];
    [self addSubview:contentView];
//    XUISplitViewContentStatus curStatus = XUISplitViewContentStatusLeft + (self.contentViews.count-1-self.leftViewIndex);
//    curStatus = curStatus > XUISplitViewContentStatusRightHide ? XUISplitViewContentStatusRightHide : curStatus;
//    if (self.contentViews.count == 2) {
//        curStatus = XUISplitViewContentStatusRightHide;
//    }

//    XUISplitViewContentStatus curStatus = XUISplitViewContentStatusRightHide;
//    if (self.contentViews.count == 2)
//        curStatus = XUISplitViewContentStatusRightHide;
//    
//    if ([self.delegate respondsToSelector:@selector(splitView:contentViewAtIndex:statusWillChangeFrom:to:)])
//    {
//        [self.delegate splitView:self contentViewAtIndex:self.contentViews.count-1 statusWillChangeFrom:curStatus to:curStatus];
//    }
//    [self contentViewAtIndex:self.contentViews.count-1 statusUpdateTo:curStatus];
//    if ([self.delegate respondsToSelector:@selector(splitView:contentViewAtIndex:statusDidChangeFrom:to:)])
//    {
//        [self.delegate splitView:self contentViewAtIndex:self.contentViews.count-1 statusDidChangeFrom:curStatus to:curStatus];
//    }
    
    {
        XUISplitViewContentStatus curStatus = XUISplitViewContentStatusNone;
        if(self.contentViews.count >= 2)
        {
            curStatus = XUISplitViewContentStatusRightHide;
            
            if(self.contentViews.count == 2)
            {
                UIView *view = self.contentViews[0];
                view.frame = contentView.frame;
            }
            
            if ([self.delegate respondsToSelector:@selector(splitView:contentViewAtIndex:statusWillChangeFrom:to:)])
            {
                [self.delegate splitView:self contentViewAtIndex:self.contentViews.count-1 statusWillChangeFrom:curStatus to:curStatus];
            }
            [self contentViewAtIndex:self.contentViews.count-1 statusUpdateTo:curStatus];
            if ([self.delegate respondsToSelector:@selector(splitView:contentViewAtIndex:statusDidChangeFrom:to:)])
            {
                [self.delegate splitView:self contentViewAtIndex:self.contentViews.count-1 statusDidChangeFrom:curStatus to:curStatus];
            }
        }
        else
        {
            contentView.frame = self.bounds;
        }
    }
}

- (void)removeLastContentView
{
    [[self.contentViews lastObject] removeFromSuperview];
    [self.contentViews removeLastObject];
}

//- (void)replaceContentViewAtIndex:(NSInteger)index withNewContentView:(UIView *)newContentView animated:(BOOL)animated
//{
//    
//}

- (void)moveContentViewAtIndexToLeft:(NSInteger)index animated:(BOOL)animated
{
    [self setLeftViewIndex:index animated:animated];
}

#pragma mark - change left view index

// 只能往前或往后移动一格
- (void)setLeftViewIndex:(NSInteger)index animated:(BOOL)animated
{
    if (index < 0 || index > self.contentViews.count-2)
    {
        return;
    }
    
    NSMutableArray *arIndex = [NSMutableArray array];
    [arIndex addObject:[NSNumber numberWithInteger:_leftViewIndex]];
    
    if(_leftViewIndex + 1 < self.contentViews.count)
        [arIndex addObject:[NSNumber numberWithInteger:_leftViewIndex + 1]];
    
    XUISplitViewContentStatus firstStatus = XUISplitViewContentStatusLeftHide;
    if (index == 0 && _leftViewIndex == 0)
    {
        firstStatus = XUISplitViewContentStatusLeft;
    }
    NSInteger plusParam = _leftViewIndex-index;
    if (index < _leftViewIndex)
    {
        [arIndex insertObject:[NSNumber numberWithInteger:index] atIndex:0];
    }
    else
    {
        firstStatus = XUISplitViewContentStatusLeft;
        if (_leftViewIndex != index)
        {
            [arIndex addObject:[NSNumber numberWithInteger:index+1]];
        }
    }
    
    if ([self.delegate respondsToSelector:@selector(splitView:contentViewAtIndex:statusWillChangeFrom:to:)])
    {
        for (NSInteger i=0; i<arIndex.count; i++)
        {
            if ([self.delegate splitView:self contentViewAtIndex:[[arIndex objectAtIndex:i] integerValue] statusWillChangeFrom:firstStatus+i to:firstStatus+i+plusParam] == NO)
            {
                return;
            }
        }
    }
    
    _leftViewIndex = index;
    [UIView animateWithDuration:splitViewAnimationDuration animations:^
    {
        for (NSInteger i=0; i<arIndex.count; i++)
        {
            [self contentViewAtIndex:[[arIndex objectAtIndex:i] integerValue] statusUpdateTo:firstStatus+i+plusParam];
        }
    } completion:^(BOOL finished)
    {
        if ([self.delegate respondsToSelector:@selector(splitView:contentViewAtIndex:statusDidChangeFrom:to:)])
        {
            for (NSInteger i=0; i<arIndex.count; i++)
            {
                [self.delegate splitView:self contentViewAtIndex:[[arIndex objectAtIndex:i] integerValue] statusDidChangeFrom:firstStatus+i to:firstStatus+i+plusParam];
            }
        }
    }];
}

- (void)contentViewAtIndex:(NSInteger)index statusUpdateTo:(XUISplitViewContentStatus)newStatus;
{
    UIView *contentView = self.contentViews[index];
    switch (newStatus)
    {
        case XUISplitViewContentStatusLeftHide:
        {
            contentView.center = CGPointMake(splitLeftViewDisplayWidth*0.8/2, self.height/2);
            contentView.transform = CGAffineTransformMakeScale(0.8, 0.8);
            contentView.backgroundColor = [UIColor colorWithWhite:0.97 alpha:1.0];
            break;
        }
        case XUISplitViewContentStatusLeft:
        {
            contentView.transform = CGAffineTransformIdentity;
            contentView.frame = CGRectMake(0, 0, self.contentViews.count == 1 ? self.width : splitLeftViewDisplayWidth, self.height);
            contentView.backgroundColor = [UIColor colorWithWhite:0.97 alpha:1.0];
            break;
        }
        case XUISplitViewContentStatusRight:
        {
            contentView.transform = CGAffineTransformIdentity;
            contentView.frame = CGRectMake(splitLeftViewDisplayWidth, 0, self.width-splitLeftViewDisplayWidth, self.height);
            contentView.backgroundColor = [UIColor colorWithWhite:1.0 alpha:1.0];
            break;
        }
        case XUISplitViewContentStatusRightHide:
        {
            contentView.transform = CGAffineTransformIdentity;
            contentView.frame = CGRectMake(self.width, 0, self.width-splitLeftViewDisplayWidth, self.height);
            contentView.backgroundColor = [UIColor colorWithWhite:1.0 alpha:1.0];
            break;
        }
        default:
            break;
    }
}

- (void)deleteLastContentViewWithAnimated:(BOOL)animated
{
    if (self.contentViews.count < 2)
    {
        return;
    }
    UIView *contentView = self.contentViews[self.contentViews.count - 2];
    UIView *lastContentView = self.contentViews[self.contentViews.count - 1];
    
    if ([self.delegate respondsToSelector:@selector(splitView:contentViewAtIndex:statusWillChangeFrom:to:)])
    {
        [self.delegate splitView:self contentViewAtIndex:self.contentViews.count - 2 statusWillChangeFrom:XUISplitViewContentStatusShrink to:XUISplitViewContentStatusExpand];
        [self.delegate splitView:self contentViewAtIndex:self.contentViews.count - 1 statusWillChangeFrom:XUISplitViewContentStatusRight to:XUISplitViewContentStatusRightHide];
    }
    
    [UIView animateWithDuration:splitViewAnimationDuration animations:^
    {
        contentView.transform = CGAffineTransformIdentity;
        contentView.frame = CGRectMake(0, 0, self.width, self.height);
        
        lastContentView.transform = CGAffineTransformIdentity;
        lastContentView.frame = CGRectMake(self.width, 0, self.width, self.height);
        contentView.backgroundColor = [UIColor colorWithWhite:1.0 alpha:1.0];
        
    } completion:^(BOOL finished)
    {
        if ([self.delegate respondsToSelector:@selector(splitView:contentViewAtIndex:statusDidChangeFrom:to:)])
        {
            [self.delegate splitView:self contentViewAtIndex:self.contentViews.count - 2 statusDidChangeFrom:XUISplitViewContentStatusShrink to:XUISplitViewContentStatusExpand];
            [self.delegate splitView:self contentViewAtIndex:self.contentViews.count - 1 statusDidChangeFrom:XUISplitViewContentStatusRight to:XUISplitViewContentStatusRightHide];
        }
    }];
}

- (void)receiveLeftSwipeGesture:(UISwipeGestureRecognizer *)swipeGesture
{
    if (self.leftViewIndex < self.contentViews.count-2 && self.contentViews.count > 2)
    {
        [self setLeftViewIndex:self.leftViewIndex+1 animated:YES];
    }
}

- (void)receiveRightSwipeGesture:(UISwipeGestureRecognizer *)swipeGesture
{
    if (self.leftViewIndex > 0)
    {
        [self setLeftViewIndex:self.leftViewIndex-1 animated:YES];
    }
}

#pragma mark -

@end
